﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ECCSystem.BizRule
{
    public class ClsCommon
    {

        // Set database connection string.
        //test environment
        //public static string gStrConnectionDB = "Data Source=WFTest1;Initial Catalog=ECCReport;User ID=ECCReport;Password=ECCReport;";
        //product environment
		//public static string gStrConnectionDB = "Data Source=192.168.197.101;Initial Catalog=ECCReport;User ID=ECCReport;Password=tropercce;";

		//public static string gStrConnectionDB = "Data Source=IISTEST01;Initial Catalog=ECCReport;User ID=ECCReport;Password=ECCReport;";

		//public static string gStrConnectionDB = "Data Source=192.168.197.101;Initial Catalog=NewECCReport;User ID=NewECCReport;Password=NewECCReport;";

		public static string gStrConnectionDB = "";
		public static int gIntTime = 0;
        public static SqlConnection gObjConn = new SqlConnection();
		
        public static string gStrOperator=string.Empty;
		
        public static int gIntOperatorID = 0;
        public static string gStrRight = string.Empty;

		public static string gstrXmlPath = "XmlPath.xml";
		public static string gStrOperatorName = string.Empty;
		public static frmMessage gfrmMessage = null;
		public static DataSet gDataSet = null;
		public static int gIntThread = 0;
		public static string gStrStart = "";
		public static string gStrEnd = "";
        public static int gintBusinessID = 0;
		public static string gstrRe = "";
		public static string gstrReLong = "";

        public ClsCommon()
        { }
		 
        public bool ConnectDBServer()
        {
            try
            {
                gObjConn.ConnectionString = gStrConnectionDB;
                gObjConn.Open(); 
                return true;
            }
            catch(Exception e )
            {
                return false;
            }
            
        }

        //only can input number by keyboard
        public void TextKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
        }

        //only non Chinese words and non Japanese words
        public bool CheckHalfSpaceStr(string strInput)
        {
            bool blFlag = true;
            string strOriginal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.";
            string strBit;
            for (int i = 0; i < strInput.Length; i++)
            {
                strBit = strInput.Substring(i, 1);
                if (strOriginal.IndexOf(strBit) >= 0)
                {
                    blFlag = true;
                }
                else
                {
                    blFlag = false;
                    break;
                }
                
            }
            return blFlag;
        }
        //Get business operation time
        public string GetECCDate(DateTime dtECCDate)
        {
            string strTempTime;
            int intPlace;

            string strTempDate = dtECCDate.Date.ToString("yyyy/MM/dd");
            strTempTime = dtECCDate.ToString("HH:mm:ss");
      
            if (dtECCDate.Hour <= 3)
            {
                DateTime dtTempDate = new DateTime();
                TimeSpan tsDiff = new TimeSpan();
                DateTime dtDate1 = new DateTime(2007, 3, 7);
                DateTime dtDate2 = new DateTime(2007, 3, 8);
                //get 1 day timespan
                tsDiff = dtDate2 - dtDate1;
                //get ECC Date
                dtTempDate = dtECCDate.Subtract(tsDiff);
                //Convert form
                strTempDate = dtTempDate.Date.ToString("yyyy/MM/dd");
                //get :mm:ss
                intPlace = strTempTime.IndexOf(':');
                strTempTime = strTempTime.Substring(intPlace, strTempTime.Length - intPlace);

                if (dtECCDate.Hour == 0)
                {
                    strTempTime = "24" + strTempTime;
                }
                if (dtECCDate.Hour == 1)
                {
                    strTempTime = "25" + strTempTime;
                }
                if (dtECCDate.Hour == 2)
                {
                    strTempTime = "26" + strTempTime;
                }
                if (dtECCDate.Hour == 3)
                {
                    strTempTime = "26" + strTempTime;
                }
            }
            else
            {

                if (dtECCDate.Hour == 7)
                {
                    //get :mm:ss
                    intPlace = strTempTime.IndexOf(':');
                    strTempTime = strTempTime.Substring(intPlace, strTempTime.Length - intPlace);
                    strTempTime = "08" + strTempTime;
                }
                //if (dtECCDate.Hour == 8)
                //{
                //    //get :mm:ss
                //    intPlace = strTempDate.IndexOf(':');
                //    strTempTime = strTempTime.Substring(intPlace, strTempDate.Length - intPlace);
                //    strTempTime = "08" + strTempTime;
                //}
                //if (dtECCDate.Hour == 9)
                //{
                //    //get :mm:ss
                //    intPlace = strTempDate.IndexOf(':');
                //    strTempTime = strTempTime.Substring(intPlace, strTempDate.Length - intPlace);
                //    strTempTime = "09" + strTempTime;
                //}
               
            }
            strTempDate = strTempDate + " " + strTempTime;
            return strTempDate;

        }

        //quit excel
        public void QuitExcelApp(Excel.ApplicationClass objName1, object objName2, object objName3, object objName4)
        {
            objName1.Workbooks.Close();
            objName1.Quit();

            if (objName4 != null)
            {
                //close Range object
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objName4);
            }
            if (objName3 != null)
            {
                //close Worksheet object
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objName3);
            }
            if (objName2 != null)
            {
                //close Workbook object
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objName2);
            }
            //close Excel object
            if (objName1 != null)
            {
                objName1.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objName1);
            }

        }
    }
}
